/**
 * 显示日期-时间
 * - smaller YYYY-MM-DD
 * - short YYYY-MM-DD HH:mm
 * - long YYYY-MM-DD HH:mm:ss
 * - point YYYY.MM.DD HH:mm:ss
 * - year YYYY
 */
function formatUtil(
  date: Date,
  mode: "smaller" | "short" | "long" | "point" | "year" | "line"
) {
  switch (mode) {
    case "smaller":
      return formatDate(date, "YYYY-MM-DD");
    case "short":
      return formatDate(date, "YYYY-MM-DD HH:mm");
    case "long":
      return formatDate(date, "YYYY-MM-DD HH:mm:ss");
    case "point":
      return formatDate(date, "YYYY.MM.DD HH:mm:ss");
    case "line":
      return formatDate(date, "YYYY/MM/DD HH:mm:ss");
    case "year":
      return formatDate(date, "YYYY");
  }
}
const formatDate = (date: Date | string, formStr: string = "YYYY-MM-DD") => {
  if (!date) return "";
  if (typeof date === "string" || typeof date == "number") {
    date = new Date(date);
  }
  let fmt = formStr;
  const t: any = {
    "M+": date.getMonth() + 1,
    "D+": date.getDate(),
    "H+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  const year = date.getFullYear() + "";
  if (/(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, year);
  }

  for (let k in t) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const val = t[k] + "";
      console.log(".....", RegExp.$1.length, "00" + val, val.length);

      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? val : ("00" + val).substring(val.length)
      );
    }
  }
  return fmt;
};

export default formatUtil;
